﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Configuration;

namespace outREACH.App_Code
{
    public class EduResource
    {
        public int ER_ID { get; set; }
        public string userName { get; set; }
        public string resourceType { get; set; }
        public string resourceName { get; set; }
        public int quantity { get; set; }
        public string country { get; set; }
        public string schoolName { get; set; }
        public DateTime dateTime { get; set; }
        public string category { get; set; }
        public string status { get; set; }
        public int partial { get; set; }

        public static void NewRequest(EduResource er)
        {
            SqlConnection conn = null;
            int newEduResourcesID = 0;

            try
            {
                conn = new SqlConnection();
                SqlCommand comm = new SqlCommand();
                conn.ConnectionString = ConfigurationManager.ConnectionStrings["outreachdbConnectionString"].ConnectionString;
                conn.Open();
                comm.Connection = conn;
                comm.CommandText = "insert into EducationalResources(userName, resourceType, resourceName, quantity, country, schoolName, dateTime, category, status, partial) VALUES (@userName, @resourceType, @resourceName, @quantity, @country, @schoolName, @dateTime, @category, @status, @partial)";
                comm.Parameters.AddWithValue("@userName", er.userName);
                comm.Parameters.AddWithValue("@resourceType", er.resourceType);
                comm.Parameters.AddWithValue("@resourceName", er.resourceName);
                comm.Parameters.AddWithValue("@dateTime", er.dateTime);
                comm.Parameters.AddWithValue("@category", er.category);
                comm.Parameters.AddWithValue("@quantity", er.quantity);
                comm.Parameters.AddWithValue("@country", er.country);
                comm.Parameters.AddWithValue("@schoolName", er.schoolName);
                comm.Parameters.AddWithValue("@status", er.status);
                comm.Parameters.AddWithValue("@partial", er.partial);
                int rowsAffected = comm.ExecuteNonQuery();
                comm.CommandText = "select @@IDENTITY as newEduResourcesID";
                SqlDataReader dr = comm.ExecuteReader();
                dr.Read();
                newEduResourcesID = int.Parse(dr["newEduResourcesID"].ToString());
                dr.Close();
            }
            catch (SqlException ex)
            {
                throw ex;
            }
            finally
            {
                conn.Close();
            }
        }

        public static List<EduResource> GetEduResource()
        {
            SqlConnection conn = null;
            List<EduResource> result = new List<EduResource>();

            try
            {
                conn = new SqlConnection();
                SqlCommand comm = new SqlCommand();
                conn.ConnectionString = ConfigurationManager.ConnectionStrings["outreachdbConnectionString"].ConnectionString;
                conn.Open();
                comm.Connection = conn;
                comm.CommandText = "Select * from EducationalResources order by [dateTime] desc";
                SqlDataReader dr = comm.ExecuteReader();
                while (dr.Read())
                {
                    EduResource er = new EduResource();
                    er.category = (string)dr["category"];
                    er.dateTime = (DateTime)dr["dateTime"];
                    er.country = (string)dr["country"];
                    er.ER_ID = (int)dr["ER_ID"];
                    er.quantity = (int)dr["quantity"];
                    er.resourceName = (string)dr["resourceName"];
                    er.resourceType = (string)dr["resourceType"];
                    er.schoolName = (string)dr["schoolName"];
                    er.userName = (string)dr["userName"];
                    er.status = (string)dr["status"];
                    er.partial = (int)dr["partial"];
                    result.Add(er);
                }
                dr.Close();
            }
            catch (SqlException ex)
            {
                throw ex;
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public static void updateEduRequest(int ER_ID, string status, int partial)
        {
            SqlConnection conn = null;
            try
            {
                conn = new SqlConnection();
                SqlCommand comm = new SqlCommand();
                conn.ConnectionString = ConfigurationManager.ConnectionStrings["outreachdbConnectionString"].ConnectionString;
                conn.Open();
                comm.Connection = conn;
                comm.CommandText = "Update EducationalResources set status = @status, partial = @partial where ER_ID = @ER_ID";
                comm.Parameters.AddWithValue("@ER_ID", ER_ID);
                comm.Parameters.AddWithValue("@status", status);
                comm.Parameters.AddWithValue("@partial", partial);
                int rowsAffected = comm.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                conn.Close();
            }
        }
    }
}